package sg.edu.nus.dbd.action;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import sg.edu.nus.dbd.util.Constants;
import sg.edu.nus.dbd.biz.DashboardBiz;
import sg.edu.nus.dbd.biz.ConfigurationBiz;
import sg.edu.nus.dbd.formbean.DetailParamData;
import sg.edu.nus.dbd.formbean.ThresholdConfig;
import sg.edu.nus.dbd.util.DateUtil;
import sg.edu.nus.dbd.util.MyLogger;

public class SecondBreakdownAction extends BaseAction {

	public ActionForward performAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		try {
			MyLogger.debug(this.getClass(), "execute::Start...");
			HttpSession session = req.getSession(false);
			String currentParam = (String) session
					.getAttribute("CurrentParameter");

			Date endDate = new Date(new java.util.Date().getTime());
			Date startDate = DateUtil.addDayToDate(endDate, -8);

			List<DetailParamData> level2DataList = new DashboardBiz()
					.getLevel2Statistics(currentParam, startDate, endDate);

			ThresholdConfig thConfig = new ConfigurationBiz()
					.getThresholdParameter(currentParam);
            System.out.println(thConfig);
			req.setAttribute("warningLevel", thConfig.getWarningLevel());
			req.setAttribute("criticalLevel", thConfig.getCriticalLevel());
			req.setAttribute("inverse", Constants.DASHBOARD_PARAMS_INVERSE
					.get(currentParam));
			req.setAttribute("startDate", DateUtil.getDateString(startDate));
			req.setAttribute("endDate", DateUtil.getDateString(endDate));
			session.setAttribute("level2DataList", level2DataList);

			return mapping.findForward("success");

		} catch (Exception e) {
			MyLogger.error(this.getClass(), e);
			return mapping.findForward("error");
		}
	}

}
